#include "Server.h"
#include "Configuration.h"

Server::Server()
{
	// load du name
	this->_name = Configuration::GetConfiguration()->GetDatabase()->SelectServerName();
	//load ban list
	IDatabase::DbResult*	result = Configuration::GetConfiguration()->GetDatabase()->SelectServerBanListLogin();
	for (unsigned int idx = 0; idx < result->size(); idx++)
		this->_banListLogin.push_back((*result->at(idx))["login"]->GetValueString());
	delete result;
}
const std::string&				Server::GetName()
{
	return this->_name;
}

void							Server::SetName(const std::string& name)
{
	//modif dans la base
	Configuration::GetConfiguration()->GetDatabase()->UpdateServerName(name);
	//modif mem
	this->_name = name;
}

const std::list<std::string>&	Server::GetBanListLogin()
{
	return this->_banListLogin;
}

bool							Server::LoginIsBan(const std::string& login)
{
	std::list<std::string>::iterator	iteCurrent = this->_banListLogin.begin();
	std::list<std::string>::iterator	iteEnd = this->_banListLogin.end();

	while (iteCurrent != iteEnd)
	{
		if ((*iteCurrent) == login)
			return true;
		++iteCurrent;
	}
	return false;
}

void							Server::AddBanListLogin(const std::string& login)
{
	this->_banListLogin.remove(login);
	//ajout dans la base
	Configuration::GetConfiguration()->GetDatabase()->InsertServerBanListLogin(login);
	this->_banListLogin.push_back(login);
}

void							Server::RemoveBanListLogin(const std::string& login)
{
	// remove de la base
	Configuration::GetConfiguration()->GetDatabase()->DeleteServerBanListLogin(login);
	this->_banListLogin.remove(login);
}

bool							Server::Authentification(const std::string login, const std::string& password)
{
	IDatabase::DbResult* result = Configuration::GetConfiguration()->GetDatabase()->SelectUser(login);
	if (result->size() && (*result->at(0))["password"]->GetValueString() == password)
		return true;
	return false;
}